using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EasyCommerce.BLL;
using EasyCommerce.DAL;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_ReviewCommentEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        ReviewComment reviewComment;

        if (Int32.TryParse(Request["id"], out id))
            reviewComment = ReviewComment.Load(id);
        else
            reviewComment = null;

        if (reviewComment != null)
        {
            tbxReviewerSiteuserId.Text = reviewComment.ReviewerSiteuserId.ToString();
			if (!string.IsNullOrEmpty(reviewComment.Comment))
				tbxComment.Text = reviewComment.Comment;
			if (reviewComment.ReviewDate != SqlDateTime.MinValue)
				tbxReviewDate.Text = reviewComment.ReviewDate.ToShortDateString();
			cbxIsVerified.Checked = reviewComment.IsVerified;
			cbxIsActive.Checked = reviewComment.IsActive;
			tbxReviewForProductId.Text = reviewComment.ReviewForProductId.ToString();

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        int reviewerSiteuserId = Security.BindIntegerInput(tbxReviewerSiteuserId.Text, _securityLevel);
		string comment = Security.BindStringInput(tbxComment.Text, _securityLevel);
		DateTime reviewDate = Security.BindDateTimeInput(tbxReviewDate.Text, _securityLevel);
		bool isVerified = cbxIsVerified.Checked;
		bool isActive = cbxIsActive.Checked;
		int reviewForProductId = Security.BindIntegerInput(tbxReviewForProductId.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (ReviewComment.Save(
				id, 
				reviewerSiteuserId,
				comment,
				reviewDate,
				isVerified,
				isActive,
				reviewForProductId
                ))
                    Response.Redirect("reviewcommentlist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            ReviewComment reviewComment = ReviewComment.Create(
				reviewerSiteuserId,
				comment,
				reviewDate,
				isVerified,
				isActive,
				reviewForProductId
                );

            if (reviewComment != null)
                Response.Redirect("reviewcommentlist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("reviewcommentlist.aspx");
    }
}
